package com.glory.cqaospda.viewmodel

import android.app.Activity
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.databinding.ObservableArrayList
import com.glory.cqaospda.R
import com.glory.cqaospda.BR
import com.glory.cqaospda.base.Constant
import com.glory.cqaospda.model.TaskRepository
import com.glory.mvvmnet.base.BaseViewModel
import com.glory.mvvmnet.entity.CallObserveBean
import com.glory.mvvmnet.entity.ToastMessageBean
import com.glorysoft.hlgdpda.utils.MyUtils
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.reflect.TypeToken
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_TabIndexGridEntity
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils
import me.tatarka.bindingcollectionadapter2.ItemBinding
import java.io.InputStream
import java.util.*
import kotlin.collections.ArrayList

class HomeFragmentModel : BaseViewModel() {

    var mfgItems = ObservableArrayList<FreeUI_TabIndexGridEntity>()
    var jurisdiction = ArrayList<String>()
    val START_ACTIVITY = 0
    private val repository by lazy { TaskRepository() }


    private val mainItemClick = object : OnItemClickListener {
        override fun onItemClick(item: Any) {

            defUI.callObserve.value = CallObserveBean(START_ACTIVITY, item)
        }
    }

    var itemBinding = ItemBinding.of<FreeUI_TabIndexGridEntity>(BR.tabIndexGrid, R.layout.item_main)
        .bindExtra(BR.itemClickListener, mainItemClick)

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    fun initAssets(context: Activity, file: String) {
        val mfgJsonStream: InputStream = context.resources.assets.open(file)
        val mfgJson = FreeApi_StringUtils.getString(mfgJsonStream)
        val mfgEntity: List<FreeUI_TabIndexGridEntity> = Gson().fromJson(
            mfgJson,
            object : TypeToken<List<FreeUI_TabIndexGridEntity?>?>() {}.type
        )
        val mEntity = ArrayList<FreeUI_TabIndexGridEntity>()

        if (file == "messagequery.txt") {
            mfgItems.clear()
            mfgItems.addAll(MyUtils.setPicToDrawable(context, mfgEntity))
        } else {
            mfgItems.clear()
            for (s in Constant.mfgList!!) {
                jurisdiction.add(s.skipclass)
                for (ss in mfgEntity) {
                    val aClass = Class.forName(ss.skipClass)
                    val name = aClass.simpleName
                    if (s.skipclass.equals(name)) {
                        val data = FreeUI_TabIndexGridEntity()
                        data.picName = ss.picName
                        data.showPlugin = ss.showPlugin
                        data.text = s.text
                        data.skipClass = ss.skipClass
                        mEntity.add(data)
                    }
                }
            }
            if (mEntity.size <= 0) {
                defUI.toastEvent.value = ToastMessageBean("当前登录人员无任何权限", false)
            }

            mfgItems.addAll(MyUtils.setPicToDrawable(context, mEntity))
        }

        Constant.IsInspectItemLongClickListener = jurisdiction.contains("CancelBuyoff")

//        {
//            "picName": "unlock",
//            "showPlugin": "header",
//            "skipClass": "com.glory.cqaospda.view.UnlockActivity",
//            "text": "解锁"
//        },
    }

    interface OnItemClickListener {
        fun onItemClick(item: Any)
    }
}